﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using LinQ_DTO;

namespace TemplateH4
{
    public partial class TimKiemSanPham : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if(!IsPostBack)
                BindData();
           
        }

        protected void btnTimNangCao_Click(object sender, EventArgs e)
        {
            
        }

        private void BindData()
        {
            LoaiSanPhamBUS LoaiSPBus = new LoaiSanPhamBUS();
            List<shop_loaisanpham> ListLoaiSP = LoaiSPBus.LayLoaiSanPham();
            shop_loaisanpham TatCaLoai = new shop_loaisanpham();
            TatCaLoai.TenLoaiSanPham = "Tất cả";
            TatCaLoai.MaLoaiSanPham = 0;
            ListLoaiSP.Add(TatCaLoai);
            CmbLoaiSP.DataSource = ListLoaiSP;
            CmbLoaiSP.DataTextField = "TenLoaiSanPham";
            CmbLoaiSP.DataValueField = "MaLoaiSanPham";
            CmbLoaiSP.DataBind();
            if (! IsPostBack)
                CmbLoaiSP.SelectedIndex = ListLoaiSP.Count - 1 ;

            NhaSanXuatBUS NSXBus = new NhaSanXuatBUS();
            List<shop_nhasanxuat> ListNhaSX = NSXBus.LayDanhSachNhaSanXuat();
            shop_nhasanxuat TatCaNSX = new shop_nhasanxuat();
            TatCaNSX.TenNhaSanXuat = "Tất Cả";
            TatCaNSX.MaNhaSanXuat = 0;
            ListNhaSX.Add(TatCaNSX);
            CmbNhaSX.DataSource = ListNhaSX;
            CmbNhaSX.DataTextField = "TenNhaSanXuat";
            CmbNhaSX.DataValueField = "MaNhaSanXuat";
            CmbNhaSX.DataBind();
            if (!IsPostBack)
                CmbNhaSX.SelectedIndex = ListNhaSX.Count - 1;

            
        }

      

        protected void BtnTimKiem_Click(object sender, ImageClickEventArgs e)
        {
            string TenSp = TxtTenSP.Text;
            SanPhamBUS SPBus = new SanPhamBUS();
            List<shop_sanpham> SearchResult = SPBus.TimSanPhamTheoTen(TenSp);

            Session["SearchSanPham"] = SearchResult;
            Response.Redirect("./ListSP.aspx?TenDS=TimKiem");

        }

        protected void btnTimKiemNC_Click(object sender, ImageClickEventArgs e)
        {
            int MaLoai = int.Parse(CmbLoaiSP.SelectedValue);
            int MaNSX = int.Parse(CmbNhaSX.SelectedValue);
            decimal LowerCost = TxtLowerCost.Text==""? 0 :decimal.Parse(TxtLowerCost.Text);
            decimal HigherCost = TxtHigherCost.Text == "" ? 1000000000 : decimal.Parse(TxtHigherCost.Text);

            SanPhamBUS SpBus = new SanPhamBUS();
            List<shop_sanpham> SearchResult = SpBus.TimSanPhamTheoLoaiGiaNhaSX(LowerCost, HigherCost, MaLoai, MaNSX);
            Session["SearchSanPham"] = SearchResult;
            Response.Redirect("./ListSP.aspx?TenDS=TimKiem");
            
        }


    }
}